package cn.jbolt.ai.app.entity;

import cn.hutool.core.util.StrUtil;
import cn.jbolt.ai.core.chain.node.base.RenderType;
import cn.jbolt.util.JSONUtil;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.jboltai.event.ThinkStatus;
import io.swagger.v3.oas.annotations.media.Schema;

import java.io.Serializable;
import java.util.Date;
import java.util.Objects;

/**
 * App消息表
 */
@TableName("ja_ai_app_message") // 指定数据库表名
public class AiAppMessage implements Serializable {
    private static final long serialVersionUID = 1L;

    public static final String STATUS_SUCCESS = "success";
    public static final String STATUS_ERROR = "error";
    public static final String STATUS_CANCEL = "cancel";

    public static final String DEFAULT_MSG = "思考中...";


    @TableId(type = IdType.ASSIGN_ID)
    private Long id;

    @Schema(name = "appSessionId", description = "关联 app会话表的主键ID")
    @TableField("app_session_id")
    private Long appSessionId;

    @Schema(name = "appId", description = "应用Id")
    @TableField("app_id")
    private Long appId;


    @Schema(name = "content", description = "消息内容")
    @TableField("content")
    private String content;
    @Schema(name = "thinking", description = "思考过程")
    @TableField("thinking")
    private String thinking;

    @Schema(name = "docs", description = "关联的AiDocs,格式[\"111\",\"222\"]")
    @TableField("docs")
    private String docs;

    @Schema(name = "imgs", description = "上传的图片,格式[\"http://112312.png\"]")
    @TableField("imgs")
    private String imgs;


    @Schema(name = "linkId", description = "关联id,消息双向关联")
    @TableField("link_id")
    private Long linkId;

    @Schema(name = "role", description = "系统角色")
    @TableField("role")
    private String role;

    @Schema(name = "status", description = "消息状态")
    @TableField("status")
    private String status;

    @Schema(name = "errorMsg", description = "错误消息原因")
    @TableField("error_msg")
    private String errorMsg;


    @Schema(name = "createTime", description = "创建时间")
    @JsonFormat(locale = "zh", pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    @TableField(value = "create_time", fill = FieldFill.INSERT)
    private Date createTime;

    @TableField(exist = false)
    private ArrayNode reference;

    @TableField(exist = false)
    private ArrayNode msgCache;



    public void appendContent(String content, RenderType renderType) {

        if (StrUtil.isBlank(content)) return;

        if (renderType == null) {
            renderType = RenderType.markdown;
        }

        if (msgCache == null) {
            msgCache = JSONUtil.createArrayNode();
        }

        msgCache.add(JSONUtil.createObjectNode("renderType", renderType.toString(), "content", content));

    }

    public void appendThinking(ThinkStatus status, String thinking) {
        if (status == ThinkStatus.START && this.thinking != null) {
            this.thinking += "\n\n ------ \n\n";
        }
        if (StrUtil.isEmpty(thinking)) return;

        if (this.thinking == null) {
            this.thinking = thinking;
        } else {
            this.thinking += thinking;
        }

    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }



    /**
     * 追加消息
     * @param content
     */
    public void appendContent(String content) {
        appendContent(content, RenderType.markdown);
    }


    public Long getLinkId() {
        return linkId;
    }

    public void setLinkId(Long linkId) {
        this.linkId = linkId;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }



    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public Long getAppSessionId() {
        return appSessionId;
    }

    public void setAppSessionId(Long appSessionId) {
        this.appSessionId = appSessionId;
    }

    public Long getAppId() {
        return appId;
    }

    public void setAppId(Long appId) {
        this.appId = appId;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getDocs() {
        return docs;
    }

    public void setDocs(String docs) {
        this.docs = docs;
    }

    public String getImgs() {
        return imgs;
    }

    public void setImgs(String imgs) {
        this.imgs = imgs;
    }

    public ArrayNode getReference() {
        return reference;
    }

    public void setReference(ArrayNode reference) {
        this.reference = reference;
    }

    public ArrayNode getMsgCache() {
        return msgCache;
    }

    public String getThinking() {
        return thinking;
    }

    public void setThinking(String thinking) {
        this.thinking = thinking;
    }

    public void setMsgCache(ArrayNode msgCache) {
        this.msgCache = msgCache;
    }
}
